package com.yunyi.web.controller.clinic.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyi.common.annotation.Anonymous;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.common.utils.StringUtils;
import com.yunyi.system.common.R;
import com.yunyi.system.domain.EmrCase;
import com.yunyi.system.service.impl.EmrCaseServiceImpl;
import com.yunyi.system.service.impl.SysDictDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author LiLing
 * @Date 2023/8/22 下午 2:31
 * @ClassName: SearchController
 * @Description: 查询 会诊病例
 * @Version 1.0
 */
@RestController
@RequestMapping("/emr")
@Anonymous
public class SearchController {

    @Autowired
    EmrCaseServiceImpl service ;

    @Autowired
    SysDictDataServiceImpl dictservice ;

    /**
     * 会诊病例 根据 条件 查询
     * @param emrCase 会诊专家id expert_id 关键字(原病理号case_no/患者姓名name)
     *                申请时间段  send_date  开始时间start / 结束时间 end
     *                诊断状态  status
     *                诊断状态 0 已诊断 1 未诊断
     * @return
     */
    @CrossOrigin
    @GetMapping("/emrcaselist")
    public R emrCaseList(EmrCase emrCase,String start,String end){
        //创建Page对象
        IPage<EmrCase> page = new Page<>(emrCase.getPage(), emrCase.getRows());
        //构建条件
        QueryWrapper qw = new QueryWrapper() ;
        //会诊专家id
        qw.eq(StringUtils.isNotBlank(emrCase.getExpert_id()),"expert_id",emrCase.getExpert_id());
        //原病理号
        qw.like(StringUtils.isNotBlank(emrCase.getCase_no()),"case_no",emrCase.getCase_no());
        //患者姓名
        qw.like(StringUtils.isNotBlank(emrCase.getName()),"name",emrCase.getName()) ;
        //会诊状态
        qw.eq(StringUtils.isNotBlank(emrCase.getStatus()),"status",emrCase.getStatus()) ;
        //申请时间段
//        qw.between("send_date",start,end) ;
        qw.ge(StringUtils.isNotBlank(start),"send_date",start)
                .le(StringUtils.isNotBlank(end),"send_date",end) ;
        //按照创建时间降序排列

        //调用方法实现 分页查询
        IPage<EmrCase> list = service.page(page,qw);

//        List<EmrCase> list = service.list();

         R r = new R().addDate("list",list.getRecords())
                .addDate("total",list.getTotal())
                .addDate("size",list.getSize())
                .addDate("current",list.getCurrent())
                .addDate("pages",list.getPages())
                ;
        return r ;
    }

    /**
     * 根据 id查询 病例详细信息
     * @param id
     * @return
     */
    @CrossOrigin
    @GetMapping("/emrcasebyid/{id}")
    public AjaxResult emrCaseById(@PathVariable String id){
        System.out.println(id);
        QueryWrapper qw = new QueryWrapper() ;
        qw.eq("id",id) ;
        return AjaxResult.success(service.list(qw));
    }

    /**
     * 根据 id 修改 修改 大体图片  诊断意见
     * @param id
     * @param gross_image
     * @param expert_diagnosis
     * @return
     */
    @CrossOrigin
    @PostMapping("/emrcaseupbyid")
    public AjaxResult emrCaseUpById(String id ,String gross_image,String expert_diagnosis){

        EmrCase emrCase = new EmrCase() ;
        System.out.println(gross_image);
        if (gross_image!=null && !gross_image.equals("")) {
            emrCase.setGross_image(gross_image);
        }
        if (expert_diagnosis!=null && !expert_diagnosis.equals("")) {
            emrCase.setExpert_diagnosis(expert_diagnosis);
        }

        boolean b = false ;
        if (id!=null && !id.equals("")){
            emrCase.setId(id);
            b = service.updateById(emrCase);
        }

        return AjaxResult.success(b) ;
    }

    /**
     * 根据id 修改状态
     * @param id
     * @param status
     * @return
     */
    @CrossOrigin
    @PostMapping("/upstatus")
    public AjaxResult upstatus(String id ,String status){
        System.out.println(id+"++"+status);
        EmrCase emrCase = new EmrCase() ;
        boolean b = false ;
        if (id!=null && !id.equals("")){
            emrCase.setId(id);
            emrCase.setStatus(status);
            b = service.updateById(emrCase);
        }

        return AjaxResult.success(b) ;
    }





}
